package jzqb.controller.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import haha.life.normal.FileUtil;
import haha.life.normal.ZtreeJsonUtils;
import jzqb.common.Result;
import jzqb.po.AdminDepartmentPo;
import jzqb.po.AdminUserPo;
import jzqb.po.ZtreeJsonPo;
import jzqb.service.AdminUserInfoService;
import jzqb.service.IZTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 *
 * @author BaiGe
 * @date: 2021/4/9 16:09
 * @Version 1.0
 */
@RestController
@RequestMapping("/admin/tree")
public class AdminTreeController {
  @Autowired
  private IZTreeService  treeService;
  @Autowired
  private AdminUserInfoService userInfoService;

    @GetMapping("/getDeptTree")
    public Result  getDeptTree(){
        List<AdminDepartmentPo> deptList = treeService.getDeptTree();
        return Result.succ(deptList);
    }

    @PostMapping("/getUsrByDeptNo")
    public Result  getUsrByDeptNo(@RequestBody String  params){
        JSONObject jsonObject = JSONObject.parseObject(params);
        String deptNo = jsonObject.getString("deptNo");
        List<AdminUserPo> userInfoList = userInfoService.findUsersByDeptNo(deptNo);
        return Result.succ(userInfoList);
    }

    @PostMapping("/getUsrByName")
    public Result  getUsrByName(@PathParam(value ="user" ) String  user){
        List<AdminUserPo> userInfoList = userInfoService.getUsrByName(user);
        return Result.succ(userInfoList);
    }

    /*
    * 2021年4月20日10:40:08
    * - cc
    * */
    @GetMapping("/getPermissionTree")
    public Result getPermissionTree(){
        ArrayList<JSONObject> res = new ArrayList<>();
        String dataBases =  FileUtil.readJsonFile("db.json");
        List<JSONObject> dataBasesList = JSON.parseArray(dataBases, JSONObject.class);

        String fileds = FileUtil.readJsonFile("domain.json");
        List<JSONObject> filedsList = JSON.parseArray(fileds, JSONObject.class);

        for (JSONObject dataBaseobj:dataBasesList){
            if (dataBaseobj.get("dataPermissionTree").equals(true)){
                JSONObject outerjson = new JSONObject();
                outerjson.put("id", dataBaseobj.get("dbCode"));
                outerjson.put("name", dataBaseobj.get("showName"));

                ArrayList<JSONObject> children = new ArrayList<>();
                for (JSONObject fieldobj:filedsList){

                    JSONObject innerjson = new JSONObject();
                    innerjson.put("id",fieldobj.get("dbCode"));
                    innerjson.put("name",fieldobj.get("showName"));

                    ArrayList<JSONObject> PDList = new ArrayList<>();
                    JSONObject childrenObj_P = new JSONObject();
                    childrenObj_P.put("id","p");
                    childrenObj_P.put("name","预览");
                    PDList.add(childrenObj_P);

                    JSONObject childrenObj_D = new JSONObject();
                    childrenObj_D.put("id","d");
                    childrenObj_D.put("name","下载");
                    PDList.add(childrenObj_D);
                    innerjson.put("children", PDList);

                    children.add(innerjson);
                }
                outerjson.put("children", children);
                res.add(outerjson);
            }
        }
        return Result.succ(res);
    }


    /*
    * 为 layui-tree 提供符合数据要求的json数据(废弃)
    * */
    @GetMapping("/getDepartmentTree4Ztree")
    public Result getDepartmentTree4Ztree(){
        List<AdminDepartmentPo> deptList = treeService.getDeptTree();
        ArrayList<ZtreeJsonPo> res = ZtreeJsonUtils.generateZtreeJson(deptList);
        return Result.succ(res);
        
//        String res =  FileUtil.readJsonFile("db_permission.json");
//        List<JSONObject> list = JSON.parseArray(res, JSONObject.class);
//        return Result.succ(list);
    }

}
